package com.dyl.manager.controller;

import cn.hutool.core.util.StrUtil;
import com.dyl.common.R;
import com.dyl.manager.entity.Brand;
import com.dyl.manager.service.IBrandService;
import com.mysql.cj.x.protobuf.MysqlxDatatypes;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.data.ConditionalOnRepositoryType;
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dyl
 * @since 2023-08-21
 */
@RestController
@RequestMapping("/manager/brand")
@RefreshScope
public class BrandController {
	@Autowired
	private IBrandService brandService;
	// @Value("${user.username}")
	// private String username;
	// @Value(("${user.age}"))
	// private Integer age;
	@Autowired
	private HttpServletRequest request;


	@GetMapping("/list")
	public R findAll(@RequestHeader("Cookie") String s) throws UnsupportedEncodingException {
		// String username = request.getHeader("username");
		// System.out.println("(manager)username = " + username);
		//经过RequestInteceptor拦截器处理后，这个cookie才会随着请求头传入进来
		// String c1 = request.getHeader("c1");
		// String c1 = request.getHeader("c1");
		// if(StrUtil.isNotBlank(c1)){
		// 	c1 = new String(c1.getBytes("iso-8859-1"),"utf-8");
		// }

		// System.out.println("c1 = " + c1);
		s = new String(s.getBytes("iso-8859-1"),"utf-8");
		System.out.println("s = " + s);

		return R.ok().put("list",brandService.list());
	}

	@GetMapping("/info")
	public R info(){
		return R.ok();//.put("username",username).put("age",age);
	}

	@GetMapping("/findById/{id}")
	public R findById(@PathVariable Long id){
		return R.ok().put("brand",brandService.getById(id));
	}

	@PostMapping("/add")
	public R add(@RequestBody Brand brand){
		brandService.save(brand);
		return R.ok();
	}
}
